package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.ListNode;

/**
 * @author jinhu
 * created 2021-07-06 16:42
 */
public class Solution_0203 {
    /**
     * 删除node.val=target的值
     *
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        //需要记录每个节点的前一个节点
        ListNode pre = new ListNode();
        pre.next = head;
        pre.val = Integer.MAX_VALUE;
        ListNode res = pre;
        while (head != null) {
            if (head.val == val) {
                ListNode param = head.next;
                pre.next = param;
                head = head.next;
            } else {
                pre = head;
                head = head.next;
            }
        }
        return res.next;
    }
}
